import 'package:blocks_blkk/components/return_button.dart';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';

import '../app_configure.dart';
import 'entity/devcie_entity.dart';
import 'entity/program_entity.dart';
import 'function_page.dart';
import 'program.dart';

class ProgrameV2Page extends FunctionPage {
  ProgramEntity? programEntity;
  DeviceEntity? deviceEntity;
  String? categoryKey;

  List<Map<ActionType, int>>? get actions {
    if (categoryKey == null ||
        deviceEntity == null ||
        deviceEntity?.actionMap == null) {
      return null;
    }

    List<Map<ActionType, int>>? l = deviceEntity!.actionMap[categoryKey];
    return l;
  }

  int? get vertical {
    if (actions == null || actions!.isEmpty) {
      return 1;
    }
    Map<ActionType, int> m = actions!.firstWhere(
      (element) => element.containsKey(ActionType.vertical),
    );
    if (m == null) {
      return 1;
    }
    return m[ActionType.vertical];
  }

  int? get horizontal {
    if (actions == null) {
      return 1;
    }
    Map<ActionType, int> m = actions!.firstWhere(
      (element) => element.containsKey(ActionType.horizontal),
    );
    if (m == null) {
      return 1;
    }
    return m[ActionType.horizontal];
  }

  ProgrameV2Page(
      {super.key, this.programEntity, this.deviceEntity, this.categoryKey});

  @override
  _ProgrameV2PageState createState() =>
      _ProgrameV2PageState(programEntity, vertical, horizontal);
}

class _ProgrameV2PageState extends ProgramPageState {
  ProgramEntity? programEntity;
  bool _playing = false;
  String? text = '';
  String? originName = '';
  int? vertical;
  int? horizontal;

  @override
  String get htmlPath {
    return 'index_3.html';
  }

  @override
  String get programeType {
    return 'programv2';
  }

  @override
  void initState() {
    super.initState();
  }

  //蓝牙图片
  String get _bluetoothImg {
    return AppConfig.instance.bluetoothStatus == 0
        ? 'assets/bluetooth_b_off.png'
        : 'assets/bluetooth_on.png';
  }

  @override
  Widget build(BuildContext context) {
    debugPrint("==================== programEntity: $programeType");
    return Container(
      child: Container(
        child: Stack(
          children: <Widget>[
            positionedWebviewController(),
            controlBtn(),
            saveDialogContent(),
            ReturnButton(
                callback: func,
                image: 'assets/return_b_btn.png',
                imagePress: 'assets/return_b_btn.png'),
            bluetootnBtn(shape: BoxShape.rectangle, image: _bluetoothImg),
          ],
        ),
      ),
    );
  }

  @override
  ProgramEntity? func() {
    return programEntity;
  }

  get playing => _playing;

  set playing(bool v) {
    _playing = v;
  }

  int getMotorAIndex() {
    return 4;
  }

  int getMotorBIndex() {
    return 7;
  }

  _ProgrameV2PageState(this.programEntity, this.vertical, this.horizontal);
}
